$(document).ready(function(){
    $.ajax({
        type : "GET",
        url : "http://localhost:8888/clazz/allClazz",
        dataType : "json",
        // contentType : "application/json",
        success : function(data) {
            console.log(data);
            for(var i in data){
                if(i==0){
                    $("#selectClazz").append("<option value="+data[i].id+" selected='selected'>"+data[i].name+"</option>");
                }else{
                    $("#selectClazz").append("<option value="+data[i].id+">"+data[i].name+"</option>");
                }
            }
        },
        error : function(xhr,textStatus) {
            console.log('错误');
            console.log(xhr);
            console.log(textStatus);
        }
    });
    $("#login").click(function(){
        window.location.href = "login.html";
    });
    $("#confirmPassword").blur(function () {
        var password=$("#password").val();
        var confirmPassword=$("#confirmPassword").val();
        if(password!=confirmPassword){
            $("#hide").show();
        }else{
            $("#hide").hide();
        }
    });
    $("#username").blur(function () {
        var name=$("#username").val();
        $.ajax({
            type : "POST",
            url : "http://localhost:8888/user/checkUser",
            dataType : "text",
            //contentType : "application/json",
            data : {
                name:name
            },
            success : function(data) {
                if("exists"==data){
                    $("#nameHide").show();
                }else{
                    $("#nameHide").hide();
                }
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });
    $("#regisiter").click(function(){
        var password=$("#password").val();
        var name=$("#username").val();
        var email=$("#email").val();
        var clazzId=$("#selectClazz").val();
        var confirmPassword=$("#confirmPassword").val();
        if(name==null||name==""||password==null||password==""||confirmPassword==null||confirmPassword==""||
            email==null||email==""){
            alert("请填写完所有信息");
            return;
        }
        $.ajax({
            type : "POST",
            url : "http://localhost:8888/user/checkUser",
            dataType : "text",
            //contentType : "application/json",
            data : {
                name:name
            },
            success : function(data) {
                if("exists"==data){
                   alert("用户名已存在!!!!")
                }else{
                    $.ajax({
                        type : "POST",
                        url : "http://localhost:8888//user/insertUser",
                        dataType : "text",
                        data : {
                            password:password,
                            email:email,
                            clazzId:clazzId,
                            name:name,
                            type:"1",
                        },
                        success : function(data) {
                            console.log(data);
                            if(data==0||password!=confirmPassword){
                                alert("两次密码输入不一致!!!");
                            }else{
                                window.location.href = "login.html";
                            }
                        },
                        error : function(xhr,textStatus) {
                            console.log('错误');
                            console.log(xhr);
                            console.log(textStatus);
                        }
                    });

                }
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });


    });
})